package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.haierp.mapper.WxUploadeMessageMapper;
import com.haierp.model.WxMessageItem;
import com.haierp.service.IWxUploadMessageService;
import com.haierp.util.DateUtil;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.PageQueryVO;


@Service("wxUploadMessageSevice")
public class WxuploadMessageServiceImpl extends SuperServiceImpl<WxUploadeMessageMapper, WxMessageItem>  implements IWxUploadMessageService {

	@Override
	public JsonPageResult<List<WxMessageItem>> queryWxMessageItem(PageQueryVO pageQueryVO) {
		JsonPageResult<List<WxMessageItem>> itemResult = new JsonPageResult<>();
		//1、查询总的记录数量
		Integer totalCount =  this.baseMapper.queryWxMessageItemCount();
		
		//2、查询分页记录
		if(totalCount!=null&&totalCount!=0){
			itemResult.buildPage(totalCount, pageQueryVO);
			List<WxMessageItem> wxMessageItems = this.baseMapper.queryWxMessageItemList(pageQueryVO);
			int imgIndex = 0;
			for(int i=0; i<wxMessageItems.size(); i++) {
				WxMessageItem messageItem = wxMessageItems.get(i);
				messageItem.setMessageTime(DateUtil.formatDate(messageItem.getSendTime()));
				String messageContent = messageItem.getMessageContent();
				String messageType = messageItem.getMessageType();
				if(messageType!=null && messageType.equals("picture")) {
					String messageContentTmp = "";
					messageContentTmp += "<a href=\"" + messageContent + "\" rel=\"gallery\" class=\"pirobox_gall item\" rev=\"" + (imgIndex++) + "\">";
					messageContentTmp += "<img src='" + messageContent + "' />";
					messageContentTmp += "</a>";
					messageItem.setMessageContent(messageContentTmp);
				} else if(messageContent.contains("http://www.qiepai.net/ExportHtml/face/")) {
					messageContent = messageContent.replaceAll("(http://www.qiepai.net/ExportHtml/face/.*?\\.gif|jpg|png)", "<img src='$1' />");
					messageItem.setMessageContent(messageContent);
				}
			}
			itemResult.setData(wxMessageItems);
		}else{
			List<WxMessageItem> wxMessageItems  = new ArrayList<>();
			itemResult.setData(wxMessageItems);
		}
		return itemResult;
	}
	
	@Override
	public void updateWxMessageSignByIds(String ids) {
		String[] idArr = ids.split(",");
		List<Long> idList =  new ArrayList<Long>();
		for(int i=0; i<idArr.length; i++) {
			idList.add(Long.parseLong(idArr[i]));
		}
		this.baseMapper.updateWxMessageSignByIds(idList);
	}
}